package com.qingsong.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qingsong.dto.*;
import com.qingsong.entity.Dept;
import com.qingsong.entity.School;
import com.qingsong.exception.ServiceException;
import com.qingsong.mapper.SchoolMapper;
import com.qingsong.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author qingsong
 */
@Service
@CacheConfig(cacheNames = "school")
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(SchoolInsertDTO schoolInsertDTO) {
        School school = BeanUtil.copyProperties(schoolInsertDTO,School.class);
        school.setDeleted(0);
        school.setCreated(LocalDateTime.now());
        school.setUpdated(LocalDateTime.now());
        int result = schoolMapper.insert(school);
        if(result <= 0){
            throw new ServiceException("数据库添加失败!");
        }
        return result;
    }

    @Cacheable(key = "#id", condition = "#id != null", unless = "#result == null")
    @Override
    public School selectById(Long id) {
        School result = schoolMapper.selectById(id);
        if(ObjectUtil.isEmpty(result)){
            throw new ServiceException("ID不存在!");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int updateById(SchoolUpdateDTO schoolUpdateDTO) {
        School school = BeanUtil.copyProperties(schoolUpdateDTO, School.class);

        // 设置默认值
        school.setCreated(null);
        school.setUpdated(LocalDateTime.now());
        // DB修改
        int result = schoolMapper.updateById(school);
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        // todo 查询该记录是否被引用
        int result = schoolMapper.logicDeleteById(id);
        if(result <= 0){
            throw new ServiceException("数据库逻辑删除失败!");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        int result = schoolMapper.logicDeleteByIds(ids);
        if(result <= 0){
            throw new ServiceException("数据库批量逻辑删除失败!");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null ")
    @Override
    public List<School> list() {
        return schoolMapper.list();
    }

    @Cacheable(key = "#root.methodName + ':' + " + "#pageDTO.getPageNum() + ':' + " + "#pageDTO.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<School> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return new PageInfo<>(schoolMapper.list());
    }

    @Cacheable(key = "#root.methodName + ':' + " +
            "#dto.getTitle() + ':' + " +
            "#dto.getPageNum() + ':' + " +
            "#dto.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<School> pageLikeTitle(SchoolPageDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        //数据分页
        return new PageInfo<>(schoolMapper.listLikeTitle(dto.getTitle()));
    }


}
